import { observable, action } from 'mobx'
import * as api from 'lib/constants/api'
import { Modal } from 'antd-mobile'
import { post } from 'lib/net'
import Toast from 'components/toast'
import Load from 'components/load'

// class AddressModel {
//   constructor (data) {
//     if (data) {
//     }
//   }
// }

const alert = Modal.alert
export default class WithdrawVM {
  constructor (props) {
    this.props = props
    this.PageInitialization()
  }

  // 获取页面信息
  @observable withdrawDetail = {}

  // 提现
  @action handleWithDraw = async () => {
    let time = new Date().getHours()
    if (this.withdrawDetail.CashType === 0) {
      alert('提现失败', '需先绑定提现账户', [
        { text: '知道了' }
      ])
    } else if (this.withdrawDetail.Banlance < 100) {
      alert('提现失败', '账户余额低于100元', [
        { text: '知道了' }
      ])
    } else if (time > 17 || time < 9) {
      alert('提现失败', '提现时间为9:00-17:00', [
        { text: '知道了' }
      ])
    } else {
      const UserId = window.localStorage.getItem('UserId')
      let response = await post(api.WZCASH, { WZId: UserId })
      if (response.Success) {
        this.props.history.push('/withdrawPrompt')
      } else {
        Toast.fail(response.Message)
      }
    }
  }

  // 选择提现方式
  @action handleToChooseWay = (CashType, BankName, BankNum, BankIcon, WXNickName, WZHeadKey) => {
    if (CashType === 2) {
      this.props.history.push(`/accountPresent?${CashType}?${BankName}?${BankNum}?${BankIcon}`)
    } else if (CashType === 1) {
      this.props.history.push(`/accountPresent?${CashType}?${WXNickName}?${WZHeadKey}`)
    } else {
      this.props.history.push(`/accountPresent`)
    }
  }

  // 跳转到提现记录页面
  @action handleRecordPresent = () => {
    this.props.history.push('/recordPresent')
  }

  // 跳转到提现规则
  @action handleWithdrawRule = () => {
    this.props.history.push('/withdrawRule')
  }

  @action PageInitialization = async () => {
    Load.loading(1, '加载中')
    const UserId = window.localStorage.getItem('UserId')
    let response = await post(api.WZUSERCASH, { WZId: UserId })
    if (response.Success) {
      Load.loading(2, '加载完成')
      this.withdrawDetail = response.Value
    } else {
      Toast.fail(response.Message)
    }
  }
}
